using System;
using System.IO;

namespace ByteShower
{
    static partial class ProgramData
    {
        public const string Error_FilePrint_0 = "No file found!";
        public const string Error_FilePrint_1 = "Empty file!";
    }
    partial class Program
    {
        static void PrintFile()
        {
            int MAXLine = Console.BufferHeight;
            if (!File.Exists(path))
                Error(ProgramData.Error_FilePrint_0 + Environment.NewLine);
            else
            {
                using (var file = File.Open(path, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite))
                {
                    if(file.Length == 0)
                        Warn(ProgramData.Error_FilePrint_1 + Environment.NewLine);
                    else
                    {
                        file.Position = 0;
                        Format("       00   01   02   03   04   05   06   07   08   09   0A   0B   0C   0D   0E   0F\n");
                        Note("0000   ");
                        int line = 0, col = 0, readN = 0, readLine = 0;
                        byte[] buff = new byte[16];
                        readN = file.Read(buff, 0, 16);
                        while(file.Position < file.Length)                    
                        {
                            if(col < 16)
                            {
                                Data($"{buff[col].ToString("X").PadLeft(2, '0')}   ");
                                col++;
                            }
                            else
                            {
                                if(readLine >= MAXLine)
                                {
                                    Info(Environment.NewLine + ProgramData.Info_Pause_0);
                                    var a = Console.ReadKey(true);
                                    if(a.Key == ConsoleKey.Escape)
                                    {
                                        readN = 0;
                                        break;
                                    }
                                    readLine = 0;
                                }
                                (line, col, readLine) = (line + 1, 0, readLine + 1);
                                readN = file.Read(buff, 0, 16);
                                Note(Environment.NewLine + $"{line.ToString("X").PadLeft(4, '0')}   ");
                                if(readN != 16)
                                    break;
                            }
                        }
                        for(int i = 0; i < readN; i++)
                            Data($"{buff[i].ToString("X").PadLeft(2, '0')}   ");   
                        if(readN != 0)
                            Data(Environment.NewLine);                     
                    }
                }
            }
        }
    }
}